#!/bin/bash
#leao:Thu Aug 22 07:15:39 PM CST 2024
index() {
    printf "这是系统主页\n"
    exit 1
}
login() {
    printf "欢迎来到登录页面\n"
    read -p "用户名
" usernameL
    read -s -p "密码
" passwordL 
    for val in `ls user`;do
        if [ $val = $usernameL ];then
            correct=`cat "user/$usernameL"`
            echo $correct
            echo $passwordL
                if [ $passwordL = $correct ]; then
                    echo "欢迎回来，${usernameL}"
                    index
                else
                    read  -p "密码错误,输入[a/A]重新登录，输入[r/R]进入注册，按任意键退出程序
" check2
                     case $check2 in
                            [a/A])
                                 login ;;
                            [r/R])
                                register;;
                                *)
                                flag= 1
                                echo "error:无效输入，自动退出"
                                exit 1;;
                        esac
        
               fi 
       
        
        fi
    done
    if test $flag -eq 1;then

     read  -p "用户名错误,输入[a/A]重新登录，输入[r/R]进入注册，按任意键退出程序
" check3
                     case $check3 in
                            [a/A])
                                 login ;;
                            [r/R])
                                register;;
                                *)
                                echo "error:无效输入，自动退出"
                                exit 1;;
                        esac
    fi
        
 }  
change() {
    echo "修改密码"

}
register() {
    printf "欢迎来到注册页面\n"
    read -p "请输入用户名
" usernameR
    read -s -p "请输入密码
" passwordR1
    read -s -p "请再次输入密码
" passwordR2
    if [ -z $usernameR ];then
        echo "用户名不能为空"
    fi
    if [ -z $passwordR1 ];then
        echo "密码不能为空"
    fi
    if [ $passwordR1 != $passwordR2 ];then
        echo "第一次密码输入和第二次密码输入不匹配，请重新输入"
        register
    else
        for val in `ls user`;do
        if [ $val = $usernameR ];then
               read -p "用户已存在
               修改密码[c/C]
               进入登录页[l/L]
               任意键退出程序
" check4
                case $check4 in
                            [c/C])
                                change ;;
                            [l/L])
                               login ;;
                                *)
                                echo "error:无效输入，自动退出"
                                exit 1;;
                esac

        fi
    done
    touch user/$usernameR
    echo "${passwordR1}" > user/$usernameR
    echo "注册成功"
    read -p "登录[l/L]
按任意键退出
" check5
    case $check5 in
            [l/L])
                login ;;
            *)
                echo "error:无效输入，自动退出"
                exit 1;;
    esac
    fi


}


read -p "欢迎来到起始页面
输入[l/L]进入登录页
输入[r/R]进入注册页
按任意键退出程序
" check1
case $check1 in
    [l/L])
        login ;;
    [r/R])
        register;;
    *)
        echo "error:无效输入，自动退出"
        exit 1;;
esac